#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "cJSON.h"

#define N 10240
#define M 10240

int main(int argc, char const *argv[])
{
    int broadfd;

    // 创建一个socket文件描述符
    broadfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (broadfd < 0)
    {
        perror("sock err");
        return -1;
    }
    // 绑定套接字(ip+port)
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(5414); // 端口号
    addr.sin_addr.s_addr = INADDR_ANY;
    int addrlen = sizeof(addr);
    if (bind(broadfd, (struct sockaddr *)&addr, addrlen) < 0)
    {
        perror("bind err");
        return -1;
    }
    ssize_t len;
    char buf[N] = {0};
    struct sockaddr_in cliaddr;

    // 接收"WhoIsNaughtyDragon"搜索包
    bzero(buf, N);
    len = recvfrom(broadfd, buf, N, 0, (struct sockaddr *)&cliaddr, &addrlen);

    // 判断是否是自己的消息：收到的数据是否"WhoIsNaughtyDragon"
    if (strcmp(buf, "WhoIsNaughtyDragon") == 0)
    {
        printf("is Four Grupe\n");
        // IAmNaughtyDragon，告诉软件，我收到了搜索协议，并且回复地址
        sendto(broadfd, "IAmNaughtyDragon", 17, 0, (struct sockaddr *)&cliaddr, addrlen);
        // 变身为TCP服务器
        int tcpfd = socket(AF_INET, SOCK_STREAM, 0);
        if (tcpfd < 0)
        {
            perror("sock err");
            return -1;
        }

        if (bind(tcpfd, (struct sockaddr *)&addr, addrlen) < 0)
        {
            perror("bind err");
            return -1;
        }
        // 监听套接字
        if (listen(tcpfd, 5) < 0)
        {
            perror("listen err");
            return -1;
        }
        // 接收客户端的连接
        printf("wait client connect\n");
        int clifd,Aclifd;
        char puf[N] = {0};
        char puf1[N]={0};
        // 接收对端的地址
        clifd = accept(tcpfd, NULL, NULL);
        if (clifd < 0)
        {
            perror("accept err");
            return -1;
        }
        printf("new cononect coming\n");
         int recv_n = 0;
         int recv_n2 = 0;

        // 接收点表
        while(1)
        {
            recv_n = recv(clifd, puf, M, 0);
            if (recv_n <0)
            {
               return -1;
            }
            cJSON *root = cJSON_Parse(puf);
            if (root == NULL)
            {
                printf("JSON parse error\n");
            }
            cJSON *item = cJSON_GetObjectItem(root, "type");
            if (item != NULL)
            {
                int type = item->valueint;
                if (type == 1)
                {
                    int Atcpfd=socket(AF_INET,SOCK_STREAM,0);
                    struct sockaddr_in Aaddr;
                    Aaddr.sin_family=AF_INET;
                    Aaddr.sin_port=htons(1333);
                    Aaddr.sin_addr.s_addr=INADDR_ANY;
                    int Aaddrlen = sizeof(Aaddr);
                   if (bind(Atcpfd, (struct sockaddr *)&Aaddr, Aaddrlen) < 0)
                   {
                   perror("bind err");
                   }
                   if(listen(Atcpfd,5)<0)
                   {
                    perror("listen err");
                   }
                   //连接接受
                   Aclifd = accept(Atcpfd, (struct sockaddr *)&cliaddr, &addrlen);
                   // 拷贝配置文件
                    int fd=open("/home/hq/4zudaxiangmu/node.json", O_WRONLY | O_CREAT | O_TRUNC, 0777);
                    if(fd<0)
                    {
                        perror("open err");
                    }
                    while(1)
                    {
                         recv_n2=recv(Aclifd,puf1,10240,0);
                         printf("%s\n",puf1);
                        if(recv_n2<=0)
                        {
                            break;
                        }
                       write(fd,puf1,recv_n2);
                       close(fd);
                       close(Aclifd);
                    }
                }
                else if (type == 3)
                {
                    if (send(clifd, "living", 7, 0) < 0)
                    {
                        perror("send err\n");
                    }
                }
            } 

            cJSON_Delete(root); // 释放 JSON 对象
        }
    }
    

    return 0;
}
